
<html lang=""><head>
    <title>模块化 · MINA</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <meta name="description" content="">
    <meta name="generator" content="GitBook 3.2.0">




    <link rel="stylesheet" href="../../../gitbook/style.css">




    <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-highlight/website.css">



    <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-search/search.css">























    <meta name="HandheldFriendly" content="true">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../../gitbook/images/favicon.ico" type="image/x-icon">


    <link rel="next" href="api.html">


    <link rel="prev" href="page.html">


</head>
<body>

<div class="book with-summary">
    <div>

        <div class="sub_nav_box">
            <div class="sub_nav_inner">




                <ul class="top_sub_nav">




                    <li class="sub_nav_item " data-level="1.1" data-path="../../">

                        <a href="../../">


                            简易教程

                        </a>

                    </li>

                    <li class="sub_nav_item selected" data-level="1.2" data-path="../MINA.html">

                        <a href="../MINA.html">


                            框架

                        </a>

                    </li>

                    <li class="sub_nav_item " data-level="1.3" data-path="../../component/">

                        <a href="../../component/">


                            组件

                        </a>

                    </li>

                    <li class="sub_nav_item " data-level="1.4" data-path="../../api/">

                        <a href="../../api/">


                            API

                        </a>

                    </li>

                    <li class="sub_nav_item " data-level="1.5" data-path="../../devtools/devtools.html">

                        <a href="../../devtools/devtools.html">


                            工具

                        </a>

                    </li>

                    <li class="sub_nav_item " data-level="1.6" data-path="../../qa/qa.html">

                        <a href="../../qa/qa.html">


                            Q&amp;A

                        </a>

                    </li>




                </ul>




                <!-- <h1>模块化</h1> -->
                <div id="book-search-input" role="search">
                    <form>
                        <label for="search-input" class="search-icon" id="js-search-icon"></label>
                        <input type="text" id="search-input" name="search-input" placeholder="搜索">
                        <button type="reset" class="clear-input-btn"></button>
                    </form>
                </div>
            </div>
        </div>

        <div class="book-summary">


            <nav role="navigation">





                <ul class="summary">


                    <li class="chapter  " data-level="1.2.1" data-path="../structure.html">

                        <a href="../structure.html">


                            目录结构

                        </a>



                    </li>

                    <li class="chapter  " data-level="1.2.2" data-path="../config.html">

                        <a href="../config.html">


                            配置

                        </a>



                    </li>

                    <li class="chapter  open" data-level="1.2.3" data-path="./">

                        <a href="./">


                            逻辑层

                        </a>



                        <ul class="articles">


                            <li class="chapter  " data-level="1.2.3.1" data-path="app.html">

                                <a href="app.html">


                                    注册程序

                                </a>



                            </li>

                            <li class="chapter  " data-level="1.2.3.2" data-path="page.html">

                                <a href="page.html">


                                    注册页面

                                </a>



                            </li>

                            <li class="chapter active " data-level="1.2.3.3" data-path="module.html">

                                <a href="module.html">


                                    模块化

                                </a>



                            </li>

                            <li class="chapter  " data-level="1.2.3.4" data-path="api.html">

                                <a href="api.html">


                                    API

                                </a>



                            </li>


                        </ul>

                    </li>

                    <li class="chapter  open" data-level="1.2.4" data-path="../view/">

                        <a href="../view/">


                            视图层

                        </a>



                        <ul class="articles">


                            <li class="chapter  " data-level="1.2.4.1" data-path="../view/wxml/">

                                <a href="../view/wxml/">


                                    WXML

                                </a>



                                <ul class="articles">


                                    <li class="chapter  " data-level="1.2.4.1.1" data-path="../view/wxml/data.html">

                                        <a href="../view/wxml/data.html">


                                            数据绑定

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.2.4.1.2" data-path="../view/wxml/conditional.html">

                                        <a href="../view/wxml/conditional.html">


                                            条件渲染

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.2.4.1.3" data-path="../view/wxml/list.html">

                                        <a href="../view/wxml/list.html">


                                            列表渲染

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.2.4.1.4" data-path="../view/wxml/template.html">

                                        <a href="../view/wxml/template.html">


                                            模板

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.2.4.1.5" data-path="../view/wxml/event.html">

                                        <a href="../view/wxml/event.html">


                                            事件

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.2.4.1.6" data-path="../view/wxml/import.html">

                                        <a href="../view/wxml/import.html">


                                            引用

                                        </a>



                                    </li>


                                </ul>

                            </li>

                            <li class="chapter  " data-level="1.2.4.2" data-path="../view/wxss.html">

                                <a href="../view/wxss.html">


                                    WXSS

                                </a>



                            </li>

                            <li class="chapter  " data-level="1.2.4.3" data-path="../view/component.html">

                                <a href="../view/component.html">


                                    组件

                                </a>



                            </li>


                        </ul>

                    </li>


                </ul>


            </nav>


        </div>

        <div class="book-body">

            <div class="body-inner">

                <!--                     
                
                <div class="book-header" role="navigation">
                    
                </div>
                
                
                 -->

                <div class="page-wrapper" tabindex="-1" role="main">
                    <div class="page-inner">

                        <div id="book-search-results">
                            <div class="search-noresults">

                                <section class="normal markdown-section">

                                    <h2 id="文件作用域">文件作用域</h2><p>在JavaScript文件中声明的变量和函数只在该文件中有效；不同的文件中可以声明相同名字的变量和函数，不会互相影响。</p><p>通过全局函数<a href="app.html#getapp"><code>getApp()</code></a>可以获取全局的应用实例，如果需要全局的数据可以在<code>App()</code>中设置，如：</p><pre><code class="lang-javascript"><span class="hljs-comment">// app.js</span>
App({
  globalData: <span class="hljs-number">1</span>
})</code></pre>
<pre><code class="lang-javascript"><span class="hljs-comment">// a.js</span>
<span class="hljs-comment">// The localValue can only be used in file a.js.</span>
<span class="hljs-keyword">var</span> localValue = <span class="hljs-string">'a'</span>
<span class="hljs-comment">// Get the app instance.</span>
<span class="hljs-keyword">var</span> app = getApp()
<span class="hljs-comment">// Get the global data and change it.</span>
app.globalData++</code></pre>
<pre><code class="lang-javascript"><span class="hljs-comment">// b.js</span>
<span class="hljs-comment">// You can redefine localValue in file b.js, without interference with the localValue in a.js.</span>
<span class="hljs-keyword">var</span> localValue = <span class="hljs-string">'b'</span>
<span class="hljs-comment">// If a.js it run before b.js, now the globalData shoule be 2.</span>
<span class="hljs-built_in">console</span>.log(getApp().globalData)</code></pre>
                                    <h2 id="模块化">模块化</h2><p>我们可以将一些公共的代码抽离成为一个单独的js文件，作为一个模块。模块只有通过<code>module.exports</code>才能对外暴露接口。</p><pre><code class="lang-js"><span class="hljs-comment">// common.js</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHello</span>(<span class="hljs-params">name</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Hello '</span> + name + <span class="hljs-string">'!'</span>)
}
<span class="hljs-built_in">module</span>.exports = {
  sayHello: sayHello
}</code></pre>
                                    <p>​在需要使用这些模块的文件中，使用<code>require(path)</code>将公共代码引入。</p><pre><code class="lang-js"><span class="hljs-keyword">var</span> common = <span class="hljs-built_in">require</span>(<span class="hljs-string">'common.js'</span>)
Page({
  helloMINA: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    common.sayHello(<span class="hljs-string">'MINA'</span>)
  }
})</code></pre>


                                </section>

                            </div>
                            <div class="search-results">
                                <div class="has-results">

                                    <h1 class="search-results-title"><span class="search-results-count"></span> 个结果 "<span class="search-query"></span>"</h1>
                                    <ul class="search-results-list"></ul>

                                </div>
                                <div class="no-results">

                                    <h1 class="search-results-title">没有找到相关内容 "<span class="search-query"></span>"</h1>

                                </div>
                            </div>
                        </div>

                    </div>
                </div>
                <div class="foot" id="footer">
                    <ul class="links ft">
                        <li class="links_item"><a href="">关于腾讯</a></li>
                        <li class="links_item"><a href="">服务协议</a></li>
                        <li class="links_item"><a href="">运营规范</a></li>
                        <li class="links_item"><a href="">辟谣中心</a></li>
                        <li class="links_item"><a href="">客服中心</a></li>
                        <li class="links_item"><a href="">联系邮箱</a></li>
                        <li class="links_item"><a href="">侵权投诉</a></li>
                        <li class="links_item">
                            <p class="copyright">Copyright © 2012-<span id="s_copyright_year"></span> Tencent. All Rights Reserved.</p>
                        </li>
                    </ul>
                </div>

            </div>



            <a href="page.html" class="navigation navigation-prev " aria-label="Previous page: 注册页面">
                <i class="fa fa-angle-left"></i>
            </a>


            <a href="api.html" class="navigation navigation-next " aria-label="Next page: API">
                <i class="fa fa-angle-right"></i>
            </a>



        </div>
    </div>


    <script src="../../../gitbook/gitbook.js"></script>
    <script src="../../../gitbook/theme.js"></script>


    <script src="../../../gitbook/gitbook-plugin-search/search-engine.js"></script>



    <script src="../../../gitbook/gitbook-plugin-search/search.js"></script>



    <script src="../../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>



    <script src="../../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>


    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"模块化","level":"1.2.3.3","depth":3,"next":{"title":"API","level":"1.2.3.4","depth":3,"path":"framework/app-service/api.md","ref":"framework/app-service/api.md","articles":[]},"previous":{"title":"注册页面","level":"1.2.3.2","depth":3,"path":"framework/app-service/page.md","ref":"framework/app-service/page.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"ignoreSpecialCharacters":false,"maxIndexSize":1000000},"theme-default":{"showLevel":false,"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}},"file":{"path":"framework/app-service/module.md","mtime":"2016-09-21T08:56:43.000Z","type":"markdown"},"gitbook":{"version":"3.2.0","time":"2016-09-22T09:40:22.799Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>




</div></body></html>